/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.controller;

import org.myontology.controller.pages.XMLPage;
import org.myontology.model.Property;
import org.myontology.model.Relation;
import org.myontology.users.User;
import org.myontology.users.UserProtocol;


/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class Update extends AbstractAction {

	private SesameSession session;
	
	/* (non-Javadoc)
	 * @see org.myontology.controller.Action#execute(org.myontology.controller.AttributeHolder)
	 */
	public void execute(AttributeHolder attributes) throws Exception {
		XMLPage.TARGET_DIV.setValue(attributes, attributes.getRequestParameter("cssidcontent"));
		User u = UserProtocol.USER.getValue(attributes);
		Property p = u.retrieveProperty(attributes.getRequestParameter("propKey"));
		String entry = attributes.getRequestParameter("entry");
		if (p.getRelation().equals(Relation.ONE_TO_MANY)) {
			String[] entries = entry.split(",");
			p.clearValues();
			for (int i=0; i<entries.length;i++) {
				p.addValue(entries[i]);
			}
		}
		else
			p.addValue(entry);
		if (p.getPredicate()!=null && u.isValid(attributes)) {
			if (p.getRelation().equals(Relation.ONE_TO_MANY)) {
				session.update(u.getURI(), p.getPredicate(), p.getValues());
			}
			else
				session.update(u.getURI(), p.getPredicate(), p.getValue());
		}
	}

}
